package org.jeecg.modules.wlhy.hyzhencarrier.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.IDUtils;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hyzhencarrier.entity.HyZhenCarrier;
import org.jeecg.modules.wlhy.hyzhencarrier.mapper.HyZhenCarrierMapper;
import org.jeecg.modules.wlhy.hyzhencarrier.service.IHyZhenCarrierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description: hy_zhen_carrier
 * @Author: jeecg-boot
 * @Date:   2023-06-19
 * @Version: V1.0
 */
@Service
public class HyZhenCarrierServiceImpl extends ServiceImpl<HyZhenCarrierMapper, HyZhenCarrier> implements IHyZhenCarrierService {

    @Autowired
    private ISysUserService sysUserService;

    @Value(value = "${customer.carrier-role-id}")
    private String carrierRoleID;

    @Override
    public Result saveZhenCarrier(HyZhenCarrier hyZhenCarrier) {


        if (hyZhenCarrier.getTel().length() != 11) {
            throw new JeecgBootException("手机号应为11位数。");
        }
        if (!hyZhenCarrier.getTel().matches(CommonConstant.REGEX_MOBILE)) {
            throw new JeecgBootException("手机号格式错误！");
        }


        SysUser user = new SysUser();
        String username = hyZhenCarrier.getTel();//设置登陆账号为手机号
//        String username = sysUserService.createRandomUserName();//随机生成账号
        user.setUsername(username);
        user.setRealname(hyZhenCarrier.getCarrierName());
        user.setPhone(hyZhenCarrier.getTel());
        //设置创建时间
        user.setCreateTime(new Date());
        String salt = oConvertUtils.randomGen(8);
        user.setSalt(salt);
        //设置默认密码123456
        user.setPassword("Aa.123456");
        String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), salt);
        user.setPassword(passwordEncode);
        user.setStatus(CommonConstant.USER_UNFREEZE);
        user.setDelFlag(CommonConstant.DEL_FLAG_0);
        user.setUType("5");
        sysUserService.addUserWithRole(user, carrierRoleID);

        hyZhenCarrier.setUserId(user.getId());
        boolean resultSave = this.save(hyZhenCarrier);
        Result<JSONObject> result = new Result<JSONObject>();
        if(resultSave){
            result.setSuccess(true);
            result.setCode(CommonConstant.SC_OK_200);
            result.setMessage("注册承运商成功！");
            result.setTimestamp((new Date()).getTime());
            return result;
        }else{
            result.setSuccess(false);
            result.setCode(CommonConstant.REGISTER_FAILED);
            result.setMessage("注册承运,请重新提交信息！");
            result.setTimestamp((new Date()).getTime());
            return result;
        }
    }
}
